#include "../global.h"

float funevaluator1(igraph_t *graph, int start, igraph_vector_t *end ,igraph_vector_t *weights){

  float choices = 0;

  igraph_vector_ptr_t ressp;
  igraph_vector_t res;
  igraph_vit_t iterator;
  igraph_vs_t vsend;
  igraph_vs_t vs;

  int nbexits = igraph_vector_size(end);
  int i = 0;

  int nodesvisited = 0;

  // res initialisation
  igraph_vector_init(&res,1);

  // ressp initialisation
  igraph_vector_ptr_init(&ressp, nbexits);
  
  for(i=0;i<nbexits;i++){
    igraph_vector_t path;
    igraph_vector_init(&path,1);
    igraph_vector_ptr_set(&ressp,i,&path);

  }

  igraph_vs_vector(&vsend, end);
  igraph_get_shortest_paths_dijkstra(graph, &ressp, start, vsend, weights, IGRAPH_ALL);
  
  for(i=0; i<nbexits; i++){
    igraph_vs_vector(&vs, igraph_vector_ptr_e(&ressp,i));
    igraph_vit_create(graph, vs, &iterator);
    while(!IGRAPH_VIT_END(iterator)){

      igraph_vs_1(&vs,IGRAPH_VIT_GET(iterator));
      
      igraph_neighborhood_size(graph, &res, vs, 1, IGRAPH_ALL); 
      int N = igraph_vector_e(&res,0) - 1;
      choices += (float) (N==1?1:N-1);
      
      IGRAPH_VIT_NEXT(iterator);
      
      nodesvisited++;
      
    }
    igraph_vit_destroy(&iterator);
  }

  igraph_vector_ptr_destroy(&ressp);
  igraph_vector_destroy(&res);

  return choices / ((float) nodesvisited);

}
